package com.xiao.xin.controller;

import com.xiao.xin.mapper.ChatHistoryMapper;
import com.xiao.xin.service.ChatHistoryService;
import com.xiao.xin.vo.ChatHistoryMemoryVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.function.Function;

@Slf4j
@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {

    @Autowired
    private ChatHistoryService chatHistoryService;

    @Autowired
    private ChatMemory chatMemory;

    /***
     * 查询历史列表
     * @param type
     * @return
     */
    @GetMapping("/{type}")
    public List<String> getChatHistoryByType(@PathVariable(name = "type") String type) {
        return chatHistoryService.getChatHistory(type);
    }

    @GetMapping("/{type}/{chatId}")
    public List<ChatHistoryMemoryVo> getChatHistory(@PathVariable("type") String type, @PathVariable("chatId") String chatId) {
        List<Message> messages = chatMemory.get(chatId);
        return messages.stream().map( message ->
                new ChatHistoryMemoryVo(message.getMessageType().getValue(), message.getText())).toList();
    }
}
